<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<% course ||= nil %>
<% if !course || !course.deleted? %>

<% master_template = course && @master_template_index && @master_template_index[course.id] %>
<% teacher_names = course && !master_template && (course.teacher_names || course.teachers.distinct.map(&:name)) %>
<% student_count = course && !master_template && (course.student_count || course.students.where("enrollments.type <> ?", "StudentViewEnrollment").count) %>

<li class="course" id="course_<%= course ? course.id : "blank" %>" style="<%= hidden unless course %>">
  <div class="course <%= 'unpublished' if course && course.read_attribute(:workflow_state) && (course.created? || course.claimed?) %>">
    <div class="name ellipsis">
      <a href="<%= course_path(course ? course.id : "{{ id }}") %>" class="name"><%= (course && course.name) || nbsp %></a>
      {{> icons/unpublished}}
    </div>
    <% if course %>
    <div class="details ellipsis">
      <div>
        <span style="padding-right: 30px;">
          <%= show_code_and_term_for(course) %>
        </span>

        <% if course.respond_to?(:sis_source_id) && course.sis_source_id %>
          <%= before_label(t(:sis_id_label, "SIS ID")) %> <%= course.sis_source_id %>
        <% end %>
      </div>
      <% if master_template %>
        <strong>{{#t}}Blueprint Course{{/t}}</strong>
        <% if master_template.last_export_completed_at %><br><%= t('Last Pushed Update: %{last_export_at}', :last_export_at => datetime_string(master_template.last_export_completed_at)) %><% end %>
        <br><%= t(:associated_count, {:one => "1 Associated Course", :other => "%{count} Associated Courses"}, :count => master_template.child_course_count) %>
      <% else %>
        <% teacher_names_new = teacher_names[0, 2] %>
        <% teacher_names_new << t(:more_count, { :one => "1 more", :other => "%{count} more" }, :count => teacher_names.length - 2) if teacher_names.length > 2 %>
        <span style="display: inline-block; min-width: 80px;"><%= t(:student_count, { :one => "1 Student", :other => "%{count} Students" }, :count => student_count || 0) %></span>
        <%= before_label(t(:teachers_label, "Teachers")) %> <%= teacher_names.blank? ? t(:none, 'none') : teacher_names_new.to_sentence %>
      <% end %>
    </div>
    <% end %>
    <div class="info">
      <span class="links">
        <%= link_to t(:settings_link, "Settings"), course_settings_path(course ? course : "{{ id }}") %> |
        <%= link_to t(:statistics_link, "Statistics"), course_statistics_path(course ? course : "{{ id }}") %> |
        <%= link_to t(:homepage_link, "Homepage"), course_path(course ? course : "{{ id }}") %>
      </span>
    </div>
  </div>
</li>
<% end %>
